<template>
  <div>
    <TopFilter :list="topFilterList" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :onCellChange="onCellChange"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="tableChangeFun"
      :onRowSelectChange="selectChange"
      :selectionType="'single'"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button size="small" type="primary" class="btnStyle" @click="submitBtn">确定 </el-button>
      <el-button size="small" type="primary" class="btnStyle" @click="closeDialog">关闭 </el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { isEmpty } from '@/utils/utils';
import { getTmPartList,getClaimPriceList } from '@/api/repair/batchEventManagement/activityDistribution';
import {findModelList } from '@/api/partsmanage/partinfos/partInfoManage/index';

import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';

export default {
  name: 'addVin',
  props: ['status', 'powerType'],
  components: {},
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getTmPartList,//车型
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      selectArr: [],
      modelList:[],
    };
  },
  components: {},
 created(){

      findModelList().then(res =>{
        this.modelList = res.data;
        this.topFilterList.find(x=>x.fieldName=='bjsycx').itemList = this.modelList;
      });


  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;

    this.getUnit();
  },
  methods: {
    async getUnit() {
      // this.getAllUnitCode(this.columns, ['unit'], 'table');
      // const res = await queryUnitCode();
      // this.columns.find(item => item.dataIndex === 'unit').dictItems  = res.data.map(x => ({ text: x.UNIT_NAME, value: x.UNIT_CODE }));
    },


    /*配置数量发生改变的时候执行*/
    onCellChange(e, obj) {
      let counts = isEmpty(obj['counts']) ? '0' : obj['counts'];
      let price = isEmpty(obj['price']) ? '0' : obj['price'];

      let toatalCount = isEmpty(parseFloat(counts) * parseFloat(price)) ? '0' : parseFloat(counts) * parseFloat(price);

      obj['amount'] = toatalCount;
    },

    tableChangeFun(val) {
            console.log(val,'tableChangeFun');

    },
    selectChange(val) {
      this.selectArr = val;
      this.selectArr.forEach(item=>{
        item.txt='',
        item.costType = 58031002,
        item.partQuantity = 1
        item.partNo = item.bjxmbh,
        item.partName = item.bjzwmc,
        item.unit = item.bjdanw,
        item.partSalesPrice = item.claimPrice

      })
      console.log(val,'peijiang');
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '备件代码',
          dataIndex: 'bjxmbh',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '备件名称',
          dataIndex: 'bjzwmc',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单位',
          dataIndex: 'bjdanw',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: []
        },

        //{
        //  title: '数量',
        //  dataIndex: 'counts',
        //  sorter: true,
        //  width: 150,
        //  editable: true,
        //  editType: 'number',
        //  defaultEditable: true,
        //  filter: true,
        //  editRequired: false,
        //  filterType: 'input',
        //  min: 0,
        //  precision: 2
        //},
        {
         title: '备件单价',
         dataIndex: 'claimPrice',
         sorter: true,
         filter: true,
         filterType: 'input',
         precision: 2,
         initialValue: ''
        },
        {
         title: '适用车型',
         dataIndex: 'bjsycxmc',
         sorter: true,
         filter: true,
         filterType: 'input',
         itemList: []
        },

      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'bjxmbh',
          placeholder: '备件代码...'
        },
        {
          type: 'INPUT',
          label: '备件名称',
          fieldName: 'bjzwmc',
          placeholder: '备件名称...'
        }
      ];
    },

    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      val.xhrAbort = false;
      this.fetchParams = Object.assign({}, val);

      console.log(this.fetchParams);
    },

    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    closeDialog() {
      this.$emit('closeVin');
    },
    submitBtn() {
      if (this.selectArr.length == 0) {
        this.$notify.warning({ title: '提示信息', message: '请您选择备件' });
        return;
      }
      this.$emit('selectArr', this.selectArr);
    }
  }
};
</script>

<style lang="less" scoped>
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}

.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}

.btn {
  text-align: center;
}

.btnStyle {
  margin-top: 10px;
}
</style>
